<script lang="ts" setup>
import tzLoading from '@/components/tz-loading.vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

const tableData = ref([])
const userStore = useUserStore()

const loading = ref(true)

const getData = () => {
  loading.value = true
  tuserControllerSellist().then(res => {
    tableData.value = res.result
    loading.value = false
  })
}
getData()

const handlePositiveClick = (row) => {
  console.log(row)

  tuserControllerDelbyid(row.id).then(res => {
    toast('删除成功')
    getData()
  })
}

const dialogVisible = ref(false)
const openDialog = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
}
const openEditDialog = (row) => {
  dialogType.value = 'edit'
  rowID.value = row.id
  Object.keys(ruleForm).forEach(v => {
    ruleForm[v] = row[v]
  })
  dialogVisible.value = true
}


const dialogType = ref('add')
const rowID = ref('')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  name: '',
  account: '',
  password: '',
})

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入厂商名称', trigger: 'blur' },
  ],
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
})


const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {


      if (dialogType.value == 'add') {
        let Q = {
          name: ruleForm.name,
          account: ruleForm.account,
          password: ruleForm.password,
        }
        tuserControllerInsert(Q).then(res => {
          console.log(res, 'add')
          toast('新增成功')
          getData()
        })
      } else {
        let Q = {
          name: ruleForm.name,
          account: ruleForm.account,
          newpassword: ruleForm.password,
          userid: userStore.userInfo.id,
          id: rowID.value
        }
        tuserControllerUpdate(Q).then(res => {
          toast('修改成功')
          getData()
        })
      }



      dialogVisible.value = false
    } else {
      console.log('error submit!', fields)
    }
  })
}


const closeFn = () => {
  Object.keys(ruleForm).forEach(v => {
    ruleForm[v] = ''
  })
  rowID.value = ''
}




</script>

<template>
  <tzLoading v-if="loading"></tzLoading>

  <div class="flex flex-col flex-1" v-else>
    <div class="h-[13%]"></div>
    <div class="pl-[25px]">
      <el-button @click="openDialog" class="w-[90px]  flex justify-center items-center" color="#009757">
        <span class="text-[16px] font-medium text-white">新增</span>
      </el-button>
    </div>
    <div class="flex-1 p-[25px]">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="产商名称" />
        <el-table-column prop="account" label="账号" />
        <el-table-column prop="password" label="密码" />
        <el-table-column prop="createTime" width="180" label="创建时间" />

        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" type="warning" @click="openEditDialog(scope.row)">
              <span>修改</span>
            </el-button>

            <n-popconfirm @positive-click="handlePositiveClick(scope.row)">
              <template #trigger>
                <el-button size="small" type="danger">
                  <span class="text-white">删除</span>
                </el-button>
              </template>
              确定要删除吗？
            </n-popconfirm>



          </template>
        </el-table-column>

      </el-table>
    </div>


    <el-dialog v-model="dialogVisible" :title="dialogType == 'add' ? '新增用户' : '编辑用户'" width="300px" top="20vh"
      @close="closeFn">
      <div class="flex justify-center gap-[30px]">
        <div class="flex-1 flex flex-col items-center">


          <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" style="width: 100%;"
            class="demo-ruleForm flex-1" status-icon>
            <el-form-item label="厂商名称" prop="name">
              <el-input style="width: 100%;" v-model="ruleForm.name" />
            </el-form-item>

            <el-form-item label="账号" prop="account">
              <el-input v-model="ruleForm.account" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password" />
            </el-form-item>
          </el-form>




        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            <span class="text-white">确定</span>
          </el-button>
        </div>
      </template>
    </el-dialog>



  </div>
</template>

<style scoped>
:deep(.el-table__row) {
  background-color: #f3fdf1;
}

:deep(th>.el-table__cell) {
  background-color: #0f914c;
}

:deep(.el-table th.el-table__cell) {
  background-color: #0e904c;
}


:deep(.el-table th.el-table__cell .cell) {
  color: #fff;
}
</style>
